﻿//in console if run this and type ticketingApp. on the console, even though it's empty, it will inherit properties from javascript library through prototype inheritance
// null > object > array 
"use strict";
var ticketingApp = (function () {
    var ticketingApp = {};
    var tickets = [];
    var firebaseURL = "https://ajaxintrovr.firebaseio.com/.json";

    var loadTickets = function(){
        $.ajax({
            firebaseURL: firebaseURL,
            type: 'GET',
        }).done (function (data){
            for (var i in data) {
                var ticket = data[i];
            }
        });

    };

    //declaring constructor functions, want to use Pascal Case instead of Camel Case
    var Create = function (issue, priority, name) { //putting directly on object instead of floating in global scope
        this.issue = issue;
        this.priority = priority;
        this.name = name;
        this.date = new Date();
    };

    var sendTicket = function () {
        //store input values into variables
        var a = document.getElementById("issue").value;
        var b = document.getElementById("priority").value;
        var c = document.getElementById("name").value;

        var t = new Create(a, b, c); //new keyword changes behavior and can be invoked multiple ways like a method or constructor; new keywork will allow it to be invoked like a constructor
        

        $.ajax({
            url: firebaseURL,
            type: 'POST',
            data: ticket //don't have to stringify b/c using jquery and with JSON in url; does it automatically
        }).done(function() {
            alert(data.name);
            tickets.push(t);
            displayTickets(); 
        });
    };

    var displayTickets = function () {
        var h = '<table class="table table-bordered table-striped">';
        h += '<tr>';
        h += '<th>Issue</th>';
        h += '<th>Priority</th>';
        h += '<th>Name</th>';
        h += '<th>Date</th>';
        h += '</tr>';

        for (var i in tickets) {//this referring to ticketingApp object
            var t = tickets[i];//direct reference to index of object
            h += '<tr>';
            h += '<td>' + t.issue + '</td>';
            h += '<td>' + t.priority + '</td>';
            h += '<td>' + t.name + '</td>';
            h += '<td>' + t.date + '</td>';
            h += '</tr>';
        }

        h += '</table>';

        document.getElementById("tickets").innerHTML = h;

    };
    loadTickets();
    return {//using closure, allows you to explose the functions you want to expose
        sendTicket: sendTicket,
        displayTickets: displayTickets
    };
})();//IIFE immediately invoked function expression: declaring it by putting entire thing in function with parenthesis and run it after at the same time.


var something = ticketingApp();
